import { createApp } from 'vue'
import pinia from '@/stores/index'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'


import * as ElementPlusIconsVue from '@element-plus/icons-vue'



const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
app.use(pinia)// 注入pinia
app.use(router)// 注入路由
app.use(ElementPlus)// 注入element-plus

app.mount('#app')



router.beforeEach(async (to) => {
  let token = sessionStorage.token; //不能用userStore.token,因为这个时候还没有初始化
  if (
    // 检查用户是否已登录
    (token === "null" || token === "" || token === undefined) &&
    // ❗️ 避免无限重定向
    to.name !== "login"
  ) {
    // 将用户重定向到登录页面
    return { name: "login" };
  }
});
